package com.yt.app.api.action;

import com.yt.app.api.action.base.BaseAction;
import com.yt.app.api.util.PatternUtil;
import com.yt.app.api.vo.BaseVo;
import com.yt.app.core.interfaces.UserContactService;
import com.yt.app.core.model.UserContact;
import com.yt.core.MsgBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import com.yt.util.IDCardCheck;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Controller
@Api(tags = "07.个人中心")
@RequestMapping("/my/contact")
public class MyContactController extends BaseAction{
    @Autowired
    private UserContactService userContactService;

    @ApiOperation(value="获取联系人", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "联系人信息", response = UserContact.class)})
    @RequestMapping("/get")
    public @ResponseBody  Object getProfile(
            @ApiParam("联系人ID") @RequestParam(name="id", required = true) Long id,
            @ApiIgnore BaseVo vo,
            @ApiIgnore HttpServletRequest request){
        MsgBean msgBean = new MsgBean();
        UserContact contact = userContactService.get(id);
        Long uid = BaseVo.getUIDFromHeader(request);
        if(contact == null || contact.getUid() != uid){
            return msgBean.failure("联系人不存在!").returnMsg();
        }
        msgBean.put("contact", contact);
        return msgBean.returnMsg();
    }

    @ApiOperation(value="联系人列表", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "联系人信息", response = UserContact.class)})
    @RequestMapping("/list")
    public @ResponseBody  Object listProfile(
            @ApiParam("上一页最后一个ID") @RequestParam(name="lastId", required = true, defaultValue = "0") Long lastId,
            @ApiIgnore BaseVo vo,
            @ApiIgnore HttpServletRequest request){
        MsgBean msgBean = new MsgBean();
        Long uid = BaseVo.getUIDFromHeader(request);
        QueryFilter filter = new DynamicSQLFilterImpl();
        filter.addFilter("Q_uid_L_EQ", String.valueOf(uid));
        if(lastId >0){
            filter.addFilter("Q_id_L_GT", String.valueOf(lastId));
        }
        List<UserContact> contactList = userContactService.find(filter);
        if(contactList != null && !contactList.isEmpty()){
            lastId = contactList.get(contactList.size() -1).getId();
        }else{
            lastId = -1L;
        }
        msgBean.put("lastId", lastId);
        msgBean.put("contacts", contactList);

        return msgBean.returnMsg();
    }

    @ApiOperation(value="保存联系人", notes = "有ID更新，否则新增", httpMethod = "POST")
    @RequestMapping("/update")
    public @ResponseBody  Object updateProfile(
            @Valid @ModelAttribute UserContact userContact,
            @ApiIgnore BaseVo vo,
            @ApiIgnore HttpServletRequest request){
        MsgBean msgBean = new MsgBean();
        Long uid = BaseVo.getUIDFromHeader(request);
        if(!IDCardCheck.validate(userContact.getIdentity()).isResult()){
            return msgBean.failure("身份证号不正确").returnMsg();
        }
//        if(!PatternUtil.USER_NAME_PATTERN.matcher(userContact.getName()).matches()){
//            return msgBean.failure("姓名不正确").returnMsg();
//        }
        if(!PatternUtil.MOBILE_PATTERN.matcher(userContact.getMobile()).matches()){
            return msgBean.failure("手机号码不正确").returnMsg();
        }
        if(userContact.getId() != null){
            userContact.setUid(uid);
            userContactService.update(userContact);
        }else{
            userContact.setUid(uid);
            userContactService.insert(userContact);
        }
        return msgBean.returnMsg();
    }

    @ApiOperation(value="删除联系人",httpMethod = "POST")
    @RequestMapping("/delete")
    public @ResponseBody  Object deleteProfile(
            @ApiParam("联系人ID") @RequestParam(name="id", required = true) Long id,
            @ApiIgnore BaseVo vo,
            @ApiIgnore HttpServletRequest request){
        MsgBean msgBean = new MsgBean();
        Long uid = BaseVo.getUIDFromHeader(request);
        UserContact contact = userContactService.get(id);
        if(contact == null || contact.getUid() != uid){
            return msgBean.failure("联系人不存在!").returnMsg();
        }
        userContactService.remove(id);
        return msgBean.returnMsg();
    }
}
